// 1.定义一个接口
interface Filter {
    filter(data: string[]): string[]
}

// 2.定义实现接口的类
class FilterA implements Filter {
    filter(data: string[]): string[] {
        return data.filter((item) => item.includes('a'))
    }
}

class FilterB implements Filter {
    filter(data: string[]): string[] {
        return data.filter((item) => item.includes('b'))
    }
}

// 3.定义一个过滤器
class FilterChain {
    filters: Filter[] = []
    addFilter(filter: Filter) {
        this.filters.push(filter)
    }
    filter(data: string[]) {
        return this.filters.reduce((res, filter) => {
            return filter.filter(res)
        }, data)
    }
}

// 4.使用过滤器
const filterChain = new FilterChain()
filterChain.addFilter(new FilterA())
filterChain.addFilter(new FilterB())
console.log(filterChain.filter(['a', 'b', 'c', 'ab']))